<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="title">
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="btnExit">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧导航栏区域 -->
      <!-- el-aside 的宽度不应该写死 应该随着折叠动态变化  -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- z左侧菜单栏折叠按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 折叠动画卡顿 直接关闭折叠动画 collapse-transition -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="currentMenu"
          router
        >
          <!-- 一级菜单 -->
          <!-- index 只接受字符串 不接受数值 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!-- index 可以用 id 代替 即 "subItem.id + ''"" -->
            <!-- 当开启el-menu的路由导航router后 index 就会作为二级菜单的路径 但是在路径里写数字不太好 应该用带有意义的路径 -->
            <!-- 这里用 subItem.path -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="activeMenu('/' + subItem.path)"
            >
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template></el-menu-item
            >
          </el-submenu>
        </el-menu></el-aside
      >
      <!-- 右侧内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      // 左侧菜单字体图标
      iconsObj: {
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      isCollapse: false,
      currentMenu: "",
    };
  },
  created() {
    this.getMenuList();
    this.currentMenu = window.sessionStorage.getItem("currentMenu");
  },
  methods: {
    btnExit() {
      // 清空 sessionStorage
      window.sessionStorage.clear();
      // 重定向到登陆页面
      this.$router.push("/login");
    },
    // 获取所有的菜单
    async getMenuList() {
      // get 返回值为promise 因此用异步async/await
      const { data: res } = await this.$axios.get("menus");
      if (res.meta.status != 200) return this.$message.error(res, meta.msg);
      this.menuList = res.data;
    },
    // 点击折叠菜单按钮
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 激活当前菜单
    activeMenu(currentMenu) {
      window.sessionStorage.setItem("currentMenu", currentMenu);
      this.currentMenu = window.sessionStorage.getItem("currentMenu");
    },
  },
};
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;

  font-size: 20px;
}
.el-header .title {
  display: flex;
  align-items: center;
}
.el-header .title img {
  margin-right: 15px;
}
.el-aside {
  background-color: #333744;
}
/* 解决左侧菜单栏对不齐 */
.el-aside .el-menu {
  border-right: none;
}
.el-aside .toggle-button {
  width: 100%;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  color: #fff;
  text-align: center;
  font-size: 10px;
  line-height: 30px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>